var app = getApp()

function formatTime(time) {
  var date = new Date(time)
  return formatDate(date)
}

function formatDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// 获取当期时间:秒数
function getTime() {
  var date = new Date()
  var timelong = date.getTime()
  return parseInt(timelong/1000)
}

function toast(title, duration) {
  wx.showToast({
    title: title,
    duration: duration ? duration : 1500,
    icon:"none"
  })
}
function successToast(title,duration){
  wx.showToast({
    title: title,
    duration: duration ? duration : 1500
  })
}
function failToast(title, duration) {
  wx.showToast({
    title: title,
    duration: duration ? duration : 1500,
    icon: "none"
  })
}

function showModal(content, cb) {
  wx.showModal({
    title: '提示',
    content: content,
    success: res=> {
      if (res.confirm) {
        if (cb) cb()
      }
    }
  })
}

function showLoading(title) {
  wx.showLoading({
    title: title,
    mask: true,
  })
}


module.exports = {
  formatNumber: formatNumber,
  formatTime: formatTime,
  formatDate: formatDate,
  getTime: getTime,
  toast:toast,
  successToast: successToast,
  failToast: failToast,
  showModal: showModal,
  showLoading: showLoading,
}